package com.personal.web.shopadmin;

import com.personal.dto.ProductCategoryExecution;
import com.personal.dto.Result;
import com.personal.entity.ProductCategory;
import com.personal.entity.Shop;
import com.personal.enums.ProductCategoryStateEnum;
import com.personal.exception.ProductCategoryOperationException;
import com.personal.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 萌新
 * @version 1.0
 * @date 2021/4/17 20:55
 */

@Controller
@RequestMapping("/shopadmin")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * 根据shopId获取productCategory
     * @param request
     * @return
     */
    @RequestMapping(value = "/getproductcategorybyshopid", method = RequestMethod.GET)
    @ResponseBody
    public Result<List<ProductCategory>> getProductCategoryList(HttpServletRequest request){
        // 在进入到
        // shop管理页面（即调用getShopManageInfo方法时）,如果shopId合法，便将该shop信息放在了session中，key为currentShop
        // 这里我们不依赖前端的传入，因为不安全。 我们在后端通过session来做
        Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
        List<ProductCategory> list = null;
        //如果这里取到的currentShop对象不为空且该对象的shopId大于0的话，
        if (currentShop != null && currentShop.getShopId() > 0) {
            list = productCategoryService.getProductCategoryList(currentShop.getShopId());
            return new Result<List<ProductCategory>>(true,list);
        }else {
            ProductCategoryStateEnum ps = ProductCategoryStateEnum.INNER_ERROR;
            return new Result<List<ProductCategory>>(false,ps.getState(), ps.getStateInfo());
        }
    }

    /**
     * 批量添加的方法
     * @param productCategoryList 参数里面加入了@requestBody这个注解，表示这个参数会自动接收前台传过来的这个productCategoryList
     * @return
     */
    @RequestMapping(value = "/addproductcategory",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addproductcategorys(@RequestBody List<ProductCategory> productCategoryList,
     HttpServletRequest request){
        //先定义一个返回值变量
        Map<String,Object> modelMap = new HashMap<String,Object>();
        //因为这里需要店铺的id来表示要添加的类别是哪个店铺的，
        //不从前端获取的原因是，用户可能会乱填数据，或者改url，加过滤器的话就比较麻烦了
        Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
        for (ProductCategory pc : productCategoryList) {
            pc.setShopId(currentShop.getShopId());
        }

        //将传过来的集合先做空值判断
        if (productCategoryList != null && productCategoryList.size() > 0) {
            try {
                ProductCategoryExecution pe = productCategoryService.batchAddProductCategory(productCategoryList);
                if (pe.getState() == ProductCategoryStateEnum.SUCCESS.getState()){
                    modelMap.put("success",true);
                    modelMap.put("effectNum",pe.getCount());
                }else {
                    modelMap.put("success",false);
                    modelMap.put("errMsg",pe.getStateInfo());
                }
            }catch (ProductCategoryOperationException e){
                modelMap.put("success",false);
                modelMap.put("errMsg",e.getMessage());
                return modelMap;
            }
        }else {
            //如果为空的话
            modelMap.put("success",false);
            modelMap.put("errMsg",ProductCategoryStateEnum.NULL_SHOP.getStateInfo());
        }
        return modelMap;
    }


    @RequestMapping(value = "/removeproductcategory",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> removeproductcategory(Long productCategoryId,HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<>();
        if (productCategoryId != null && productCategoryId > 0){
            try {
                Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
                ProductCategoryExecution pe = productCategoryService.deleteProductCategory(productCategoryId, currentShop.getShopId());
                if (pe.getState() == ProductCategoryStateEnum.SUCCESS.getState()){
                    modelMap.put("success",true);
                }else {
                    modelMap.put("success",false);
                    modelMap.put("errMsg",pe.getStateInfo());
                }
            }catch (RuntimeException e){
                modelMap.put("success",false);
                modelMap.put("errMsg",e.toString());
                return modelMap;
            }
        }else {
            modelMap.put("success",false);
            modelMap.put("errMsg", "请至少选择一个商品类别！");
        }

        return modelMap;
    }
}
